package util.webSearch;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import puppy.db.DBHandler;

import util.io.FileInput;
import util.parser.DeliciousURLInfoPost;


/**
 * 
 * @author Sergio Duarte
 *
 */
public class WebDocumentCollector {

	/**
	 * @param args
	 */
	
	
	public static HashSet<String> url = new HashSet<String>();
	public static Hashtable<String,HashSet<String>> hash= new Hashtable<String,HashSet<String>>();
	public static final int  results=20;

	
	
	public static void webCollector(String pathQueries) {

		FileInput input = new FileInput(pathQueries);

		String line = input.readString();

		int lines = 0;
		int total = 0;
		while (line != null) {

			HashSet<String> set = new HashSet<String>();

			line = input.readString();

			if (line != null) {
				LinkedList<WebResult> list1 = GoogleQuery.googleQuery(line
						.trim(), results);
				LinkedList<WebResult> list2 = YahooQuery.yahooQuery(
						line.trim(), results);
				LinkedList<WebResult> list3 = BingQuery.bingQuery(line.trim(),
						results);
				LinkedList<WebResult> list4 = new LinkedList<WebResult>();

				list4.addAll(list1);
				list4.addAll(list2);
				list4.addAll(list3);

				addQueryInfo(list4, line);

				DBHandler.insertWebResults(list4);

				extractURL(list1, set);
				extractURL(list2, set);
				extractURL(list3, set);
				total += set.size();
				lines++;
				hash.put(line.trim(), set);
				System.out.println(line);
				printSet(set);

			}
		}

		System.out.println("\nStat:" + (double) total / lines);

	}
	
	
	

	
	private static void addQueryInfo(LinkedList<WebResult> list3, String query) {
		// TODO Auto-generated method stub
		
		
		Long id= DBHandler.getQueryID(query);
		
		for(int i=0; i < list3.size();i++){
			
			list3.get(i).setQuery(query);
			list3.get(i).setQueryId(id);
			
			
		}
		
	}


	public static void printSet(HashSet<String> set) {
		Iterator<String> iter = set.iterator();

		while (iter.hasNext()) {

			String line = iter.next();

			System.out.println("\t"+line);

		}

	}
	
	
	
	/**
	 *   
	 * @param query
	 * @param searchEngine
	 * @param results
	 * @param limit
	 */
	public static void querySearchEngine(String query, int searchEngine,
			LinkedList<WebResult> list, int limit, TagAggregator tags) {

		if (searchEngine == Constants.BING) {

			list.addAll(BingQuery.bingQuery(query, limit));
		}

		if (searchEngine == Constants.GOOGLE) {
			list.addAll(GoogleQuery.googleQuery(query, limit));

		}

		if (searchEngine == Constants.YAHOO) {
			list.addAll(YahooQuery.yahooQuery(query, limit));

		}

	//	tags.updateTags(list);

	}
	
	public static void extractURL(LinkedList<WebResult> list,
			HashSet<String> set) {

		Iterator<WebResult> iter = list.iterator();

		while (iter.hasNext()) {

			WebResult result = iter.next();

			set.add(result.getUrl());

		}

	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String path="/home/sergio/queries.txt";

		TagAggregator tags= new TagAggregator();
		LinkedList<WebResult> list = new LinkedList<WebResult>();
		
		querySearchEngine("elmo games", Constants.GOOGLE, list, 3, tags);
		System.out.println(list.size());
		
	}

}
